﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using SmallERP.COM;
using SmallERP.MODEL.FenPian;
using SmallERP.MODEL.FoundationModel;
using SmallERP.DAL;
using System.Data.Entity;
using System.Configuration;
using Smallerp.Contracts;
using Smallerp.Contracts.DataContracts;




namespace SmallERP.Bll
{
    public class FenPianBLL
    {
        IOperateDBFenPian _IOperateDB;
        public FenPianBLL(IOperateDBFenPian inOp)
        {
            _IOperateDB = inOp;
        }
        //smallerpDataContext dataBase = new smallerpDataContext();
        public List<GridSubArea_Info> GetAllSubAreaByType()
        {

            List<GridSubArea_Info> result = new List<GridSubArea_Info>();
            //using (smallerpDataContext db = new smallerpDataContext())
            //{
            //    foreach (var s in db.GridSubArea.ToList())
            //    {
            //        if (result == null)
            //        {
            //            result = new List<GridSubArea_Info>();
            //        }
            //        result.Add(new GridSubArea_Info()
            //        {
            //            SubAreaID = s.SubAreaID,

            //            HeightNum = s.heightNum,
            //            OutTimeAlarmTime = s.OutTimeAlarmTime == null ? 0 : (int)s.OutTimeAlarmTime,
            //            SubAreaName = s.SubAreaName,
            //            WidthNum = s.widthNum
            //        });
            //    }
            //}
            string QueryGridAreas = "select * from GridSubArea";
            DataTable table = _IOperateDB.QueryDataTable(QueryGridAreas);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                GridSubArea_Info info=new GridSubArea_Info();
                            info.SubAreaID =  Convert.ToInt32(table.Rows[i]["SubAreaID"]);

                            info.HeightNum =  Convert.ToInt32(table.Rows[i]["HeightNum"]);;
                            info.OutTimeAlarmTime = Convert.ToInt32(table.Rows[i]["OutTimeAlarmTime"]);
                            info.SubAreaName = Convert.ToString(table.Rows[i]["SubAreaName"]);
                            info.WidthNum = Convert.ToInt32(table.Rows[i]["WidthNum"]);
                            result.Add(info);
            }
                return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="subAreaId"></param>
        /// <param name="GridInfo_GridList"></param>
        /// <returns></returns>
        public int GetGridInfoBySubAreaId(int inSubAreaId, out List<GridInfo_GridDCT> GridInfo_GridList)
        {
            int result = 0;
            //List<Grid_Info> GridBaseInfoList;
            Order_Info oG = null;
            // GridInfo_Grid ginfo = null;

            List<GridInfo_GridDCT> rtnList = new List<GridInfo_GridDCT>();

            if (inSubAreaId > 0)
            {
                GridInfo_GridList = new List<GridInfo_GridDCT>();
                //using (smallerpDataContext db = new smallerpDataContext())
                //{
                //    List<Grid> gridList = db.Grid.Where(p => p.GridSubArea.SubAreaID == inSubAreaId).ToList();
                //    if (gridList.Count < 1)
                //    {
                //        result = -1; //空分区 未划分格子
                //    }
                //    else
                //    {
                //        rtnList = ConvertToList(gridList, db);
                //        result = 0;
                //    }


                //}
                rtnList = ConvertToList(inSubAreaId);

            }
            GridInfo_GridList = rtnList;

            return result;
        }
        /// <summary>
        /// 转化成现实ListS
        /// </summary>
        /// <param name="inList"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public List<GridInfo_GridDCT> ConvertToList(int inSubAreasid)
        {
            List<GridInfo_GridDCT> returnList = new List<GridInfo_GridDCT>();
            GridInfo_GridDCT gridInfoGrid = new GridInfo_GridDCT();
            //foreach (Grid grid in inList.OrderBy(P => P.GridName.Substring(1)).ToList())
            //{
            //    gridInfoGrid = new GridInfo_Grid();

            //    orderInfo orderProduct = db.orderInfo.FirstOrDefault(p => p.OrderProductId == grid.OrderID);
            //    if (orderProduct != null)
            //    {
            //        orderProduct.photoInfo.Load();
            //        OrderView product = new OrderView() { CompleteTime = orderProduct.CompleteTime, OrderName = orderProduct.OrderName, OrderPath = orderProduct.OrderPath, OrderProductId = orderProduct.OrderProductId, Priority = orderProduct.Priority, ProductNum = orderProduct.ProductNum, Remark = orderProduct.Remark, StartTime = orderProduct.StartTime, Status = orderProduct.Status };
            //        gridInfoGrid.OrderInfo = product;
            //        PaperFace face = db.PaperFace.FirstOrDefault(p => p.PaperFaceId == orderProduct.PaperFaceId);
            //        PaperType type = db.PaperType.FirstOrDefault(p => p.PaperTypeId == orderProduct.PaperTypeId);
            //        List<photoInfo> photoList = orderProduct.photoInfo.ToList();
            //        List<PhotoView> listPhotoInfo = new List<PhotoView>();
            //        PhotoView info;
            //        foreach (photoInfo list in photoList)
            //        {
            //            info = new PhotoView() { Height = list.Height, Width = list.Width, IsReback = list.IsReback, OrderProductId = list.orderInfo.OrderProductId, 
            //PhotoId = list.PhotoId, PhotoName = list.PhotoName, PicPath = list.PicPath, PixHeight = list.PixHeight, PixWidth = list.PixWidth, Status = list.Status };
            //            if (info.Height < info.Width)
            //            {
            //                info.SizeInch = info.Height + "X" + info.Width;
            //                info.SizeCM = info.Height * 2.54 + "X" + info.Width * 2.54;
            //            }
            //            else
            //            {
            //                info.SizeInch = info.Width + "X" + info.Height;
            //                info.SizeCM = info.Width * 2.54 + "X" + info.Width * 2.54;
            //            }
            //            info.PaperFace = face.PaperFaceName;
            //            info.PaperType = type.PaperTypeName;
            //            if (info.PrinterId != 0)
            //            {
            //                info.PrinterName = db.printer.FirstOrDefault(p => p.PrinterID == info.PrinterId).PName;
            //            }

            //            listPhotoInfo.Add(info);
            //        }
            //        product.PhotoS = listPhotoInfo;
            //    }
            //    else
            //    {
            //        gridInfoGrid.OrderInfo = null;
            //    }

            //    // List<photoInfo> photoList = orderProduct.photoInfo.ToList();
            //    Grid_Info gridInfo = new Grid_Info() { GridId = grid.GridId, FirstEnterTime = grid.FirstEnterTime, GridName = grid.GridName, OrderID = grid.OrderID, Status = grid.Status };

            //    gridInfoGrid.GridBaseInfo = gridInfo;
            //    returnList.Add(gridInfoGrid);

            //}
            string queryGrid = "select *  from grid where  SubAreaId=" + inSubAreasid + " order by Val(mid(gridname,2,len(gridname)-1))";
            DataTable GridTable = _IOperateDB.QueryDataTable(queryGrid);
            if (GridTable.Rows.Count > 0)
            {
                for (int i = 0; i < GridTable.Rows.Count; i++)
                {
                    gridInfoGrid = new GridInfo_GridDCT();
                    string QueryOrderinfo = "select * from orderinfo,papertype,paperface where orderinfo.papertypeid=papertype.papertypeid and paperface.paperfaceid=orderinfo.paperfaceid and orderproductid=" + Convert.ToInt32(GridTable.Rows[i]["OrderID"]);
                    DataTable tableOrder = _IOperateDB.QueryDataTable(QueryOrderinfo);
                    if (tableOrder.Rows.Count > 0)
                    {
                        OrderViewDCT product = new OrderViewDCT() { CompleteTime = Convert.ToDateTime(tableOrder.Rows[0]["CompleteTime"]), OrderName = Convert.ToString(tableOrder.Rows[0]["OrderName"]), OrderPath = Convert.ToString(tableOrder.Rows[0]["OrderPath"]), OrderProductId = Convert.ToInt32(tableOrder.Rows[0]["OrderProductId"]), Priority = Convert.ToInt32(tableOrder.Rows[0]["Priority"]), ProductNum = Convert.ToInt32(tableOrder.Rows[0]["ProductNum"]), Remark = Convert.ToString(tableOrder.Rows[0]["Remark"]), StartTime = Convert.ToDateTime(tableOrder.Rows[0]["StartTime"]), Status = Convert.ToInt32(tableOrder.Rows[0]["Status"]) };
                        gridInfoGrid.OrderInfo = product;


                        List<PhotoDCT> listPhotoInfo = new List<PhotoDCT>();
                        string QueryPhoto = "select * from photoinfo where orderproductid=" + Convert.ToInt32(tableOrder.Rows[0]["OrderProductId"]);
                        DataTable tablePhoto = _IOperateDB.QueryDataTable(QueryPhoto);
                        PhotoDCT info;
                        for (int j = 0; j < tablePhoto.Rows.Count; j++)
                        {
                            info = new PhotoDCT()
                            {
                                Height = Convert.ToInt32(tablePhoto.Rows[j]["Height"]),
                                Width = Convert.ToInt32(tablePhoto.Rows[j]["Width"]),
                                IsReback = Convert.ToBoolean(tablePhoto.Rows[j]["IsReback"]),
                                OrderProductId = Convert.ToInt32(tableOrder.Rows[0]["OrderProductId"]),
                                PhotoId = Convert.ToInt32(tablePhoto.Rows[j]["PhotoId"]),
                                PhotoName = Convert.ToString(tablePhoto.Rows[j]["PhotoName"]),
                                PicPath = Convert.ToString(tablePhoto.Rows[j]["PicPath"]),
                                PixHeight = Convert.ToInt32(tablePhoto.Rows[j]["PixHeight"]),
                                PixWidth = Convert.ToInt32(tablePhoto.Rows[j]["PixWidth"]),
                                Status = Convert.ToInt32(tablePhoto.Rows[j]["Status"]),
                                PrinterId = Convert.ToInt32(tablePhoto.Rows[j]["PrinterId"])

                            };
                            if (info.Height < info.Width)
                            {
                                info.SizeInch = info.Height + "X" + info.Width;
                                info.SizeCM = info.Height * 2.54 + "X" + info.Width * 2.54;
                            }
                            else
                            {
                                info.SizeInch = info.Width + "X" + info.Height;
                                info.SizeCM = info.Width * 2.54 + "X" + info.Width * 2.54;
                            }
                            info.PaperFace = Convert.ToString(tableOrder.Rows[0]["PaperFaceName"]);
                            info.PaperType = Convert.ToString(tableOrder.Rows[0]["PaperTypeName"]);
                            if (info.PrinterId != 0)
                            {
                                info.PrinterName = (string)_IOperateDB.ExecuteScalar("select PName from printer where printerid=" + info.PrinterId);
                            }
                            listPhotoInfo.Add(info);

                        }
                        product.PhotoS = listPhotoInfo;
                    
                      }
                    else
                    {
                        gridInfoGrid.OrderInfo = null;
                    }
                    Grid_InfoDCT gridInfo = new Grid_InfoDCT() { GridId = (int)GridTable.Rows[i]["GridId"], FirstEnterTime = (DateTime)GridTable.Rows[i]["FirstEnterTime"], GridName = (string)GridTable.Rows[i]["GridName"], OrderID = (int)GridTable.Rows[i]["OrderID"], Status = (int)GridTable.Rows[i]["Status"] };
                    gridInfoGrid.GridBaseInfo = gridInfo;
                    returnList.Add(gridInfoGrid);
                }
                   
            }
            else
            {
                gridInfoGrid.OrderInfo = null;
            }
                return returnList;
        }
        /// <summary>
        /// 转化实体
        /// </summary>
        /// <param name="inGridID"></param>
        /// <param name="orderProductid"></param>
        /// <returns></returns>
        public GridInfo_GridDCT ConvertToView(int inGridID, int orderProductid)
        {
            GridInfo_GridDCT gridInfoGrid = new GridInfo_GridDCT();
            if (orderProductid != null && orderProductid!=0)
            {
                string QueryOrderinfo = "select * from orderinfo,papertype,paperface where orderinfo.papertypeid=papertype.papertypeid and paperface.paperfaceid=orderinfo.paperfaceid and orderproductid=" + orderProductid;
                DataTable tableOrder = _IOperateDB.QueryDataTable(QueryOrderinfo);
                OrderViewDCT product = new OrderViewDCT() { CompleteTime = Convert.ToDateTime(tableOrder.Rows[0]["CompleteTime"]), OrderName = Convert.ToString(tableOrder.Rows[0]["OrderName"]), OrderPath = Convert.ToString(tableOrder.Rows[0]["OrderPath"]), OrderProductId = Convert.ToInt32(tableOrder.Rows[0]["OrderProductId"]), Priority = Convert.ToInt32(tableOrder.Rows[0]["Priority"]), ProductNum = Convert.ToInt32(tableOrder.Rows[0]["ProductNum"]), Remark = Convert.ToString(tableOrder.Rows[0]["Remark"]), StartTime = Convert.ToDateTime(tableOrder.Rows[0]["StartTime"]), Status = Convert.ToInt32(tableOrder.Rows[0]["Status"]) };
                gridInfoGrid.OrderInfo = product;
                List<PhotoDCT> listPhotoInfo = new List<PhotoDCT>();
                string QueryPhoto = "select * from photoinfo where orderproductid=" + Convert.ToInt32(tableOrder.Rows[0]["OrderProductId"]);
                DataTable tablePhoto = _IOperateDB.QueryDataTable(QueryPhoto);
                PhotoDCT info;
                for (int j = 0; j < tablePhoto.Rows.Count; j++)
                {
                    info = new PhotoDCT()
                    {
                        Height = Convert.ToInt32(tablePhoto.Rows[j]["Height"]),
                        Width = Convert.ToInt32(tablePhoto.Rows[j]["Width"]),
                        IsReback = Convert.ToBoolean(tablePhoto.Rows[j]["IsReback"]),
                        OrderProductId = Convert.ToInt32(tablePhoto.Rows[0]["OrderProductId"]),
                        PhotoId = Convert.ToInt32(tablePhoto.Rows[j]["PhotoId"]),
                        PhotoName = Convert.ToString(tablePhoto.Rows[j]["PhotoName"]),
                        PicPath = Convert.ToString(tablePhoto.Rows[j]["PicPath"]),
                        PixHeight = Convert.ToInt32(tablePhoto.Rows[j]["PixHeight"]),
                        PixWidth = Convert.ToInt32(tablePhoto.Rows[j]["PixWidth"]),
                        Status = Convert.ToInt32(tablePhoto.Rows[j]["Status"]),
                        PrinterId = Convert.ToInt32(tablePhoto.Rows[j]["PrinterId"])

                    };
                    if (info.Height < info.Width)
                    {
                        info.SizeInch = info.Height + "X" + info.Width;
                        info.SizeCM = info.Height * 2.54 + "X" + info.Width * 2.54;
                    }
                    else
                    {
                        info.SizeInch = info.Width + "X" + info.Height;
                        info.SizeCM = info.Width * 2.54 + "X" + info.Width * 2.54;
                    }
                    info.PaperFace = Convert.ToString(tableOrder.Rows[0]["PaperFaceName"]);
                    info.PaperType = Convert.ToString(tableOrder.Rows[0]["PaperTypeName"]);
                    if (info.PrinterId != 0)
                    {
                        info.PrinterName = (string)_IOperateDB.QueryDataTable("select PName from printer where printerid=" + info.PrinterId).Rows[0]["PName"];
                    }
                    listPhotoInfo.Add(info);

                }
                product.PhotoS = listPhotoInfo;
               // Grid_Info gridInfo = new Grid_Info() { GridId = (int)GridTable.Rows[i]["GridId"], FirstEnterTime = (DateTime)GridTable.Rows[i]["FirstEnterTime"], GridName = (string)GridTable.Rows[i]["GridName"], OrderID = (int)GridTable.Rows[i]["OrderID"], Status = (int)GridTable.Rows[i]["Status"] };
                //gridInfoGrid.GridBaseInfo = gridInfo;
            }
            else
            {
                gridInfoGrid.OrderInfo=null;
            }

            if (inGridID != null && inGridID != 0)
            {
                string queryGrid = "select *  from grid  where gridid=" + inGridID;
                DataTable GridTable = _IOperateDB.QueryDataTable(queryGrid);
                if (GridTable.Rows.Count > 0)
                {
                    Grid_InfoDCT gridInfo = new Grid_InfoDCT() { GridId = (int)GridTable.Rows[0]["GridId"], FirstEnterTime = (DateTime)GridTable.Rows[0]["FirstEnterTime"], GridName = (string)GridTable.Rows[0]["GridName"], OrderID = (int)GridTable.Rows[0]["OrderID"], Status = (int)GridTable.Rows[0]["Status"] };
                    gridInfoGrid.GridBaseInfo = gridInfo;
                }
                else
                {
                    gridInfoGrid.GridBaseInfo = null;
                }
 
            }
            return gridInfoGrid;

        }
       
        /// <summary>
        /// 照片分配格子
        /// </summary>
        /// <param name="inPhotoID"ID</param>
        /// <param name="OutGridName">格子名字</param>
        /// <param name="outGridInfo_Grid">刷新界面的实体</param>
        /// <returns></returns>
        public int AssinGrid(int inPhotoID, out string OutGridName, out GridInfo_GridDCT outGridInfo_Grid, out int outGridID, int inSubAreaID)
        {

           return  _IOperateDB.AssinGrid(inPhotoID,out OutGridName,out outGridInfo_Grid, out outGridID,inSubAreaID);
        }
        /// <summary>
        /// 照片分配格子
        /// </summary>
        /// <param name="inPhotoID"ID</param>
        /// <param name="OutGridName">格子名字</param>
        /// <param name="outGridInfo_Grid">刷新界面的实体</param>
        /// <returns></returns>
        public int AssinGridForAutoCut(int inPhotoID, out string OutGridName, out GridInfo_GridDCT outGridInfo_Grid, out int outGridID, int inSubAreaID)
        {
            return _IOperateDB.AssinGrid(inPhotoID, out OutGridName, out outGridInfo_Grid, out outGridID, inSubAreaID);

            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inGridid"></param>
        /// <returns></returns>
        public int ForcePrintGrid(GridInfo_GridDCT gridInfo)
        {
            return _IOperateDB.ForcePrintGrid(gridInfo);
        }
        /// <summary>
        /// 情况格子信息
        /// </summary>
        /// <param name="inGrid"></param>
        /// <returns></returns>
        public int ClearSelectedGrid(GridInfo_GridDCT inGrid)
        {
            
            return _IOperateDB.ClearSelectedGrid(inGrid);
        }
        /// <summary>
        /// 获取复检实体
        /// </summary>
        /// <param name="inPhotoId"></param>
        /// <returns></returns>
        public GridInfo_GridDCT GetReCheckView(int inPhotoId)
        {
            GridInfo_GridDCT grid = new GridInfo_GridDCT();
            //using (smallerpDataContext db = new smallerpDataContext())
            //{
            //    photoInfo photo = db.photoInfo.FirstOrDefault(p => p.PhotoId == inPhotoId);
            //    photo.orderInfoReference.Load();
            //    orderInfo order = photo.orderInfo;
            //    grid = ConvertToView(null, order, db);

            //}
            string QueryPhoto = "select orderproductid from photoinfo where photoid=" + inPhotoId;
            grid=ConvertToView(0, (int)_IOperateDB.ExecuteScalar(QueryPhoto));
            return grid;
        }
        /// <summary>
        /// 清柜
        /// </summary>
        /// <param name="infoGrids"></param>
        /// <param name="inNum"></param>
        /// <returns></returns>
        public int PriorityPhoto(List<GridInfo_GridDCT> infoGrids, int inNum)
        {
            return _IOperateDB.PriorityPhoto(infoGrids, inNum);
            //using (smallerpDataContext db = new smallerpDataContext())
            //{

            //    try
            //    {
            //        foreach (GridInfo_Grid list in infoGrids)
            //        {
            //            List<PhotoView> photoList =
            //                list.OrderInfo.PhotoS.Where(
            //                    p =>
            //                    p.Status != (int)Status.PhotoStatus.Printed &&
            //                    p.Status != (int)Status.PhotoStatus.InGrid).ToList();
            //            if (inNum == 0)
            //            {
            //                //等于0为不限制
            //            }
            //            else
            //            {
            //                if (photoList.Count > inNum)
            //                {
            //                    continue;//大于限制条件的不做处理
            //                }
            //            }
            //            foreach (PhotoView view in photoList)
            //            {
            //                photoInfo photo = db.photoInfo.FirstOrDefault(p => p.PhotoId == view.PhotoId);

            //                photo.PicMakeUp.Load();
            //                List<PicMakeUp> picMakeUpList = photo.PicMakeUp.ToList();
            //                foreach (PicMakeUp up in picMakeUpList)
            //                {
            //                    up.BoardReference.Load();
            //                    if (up.Board.Status != (int)Status.EnumBoardStatus.Printed)
            //                    {
            //                        up.BoardReference.Load();
            //                        Board board = up.Board;
            //                        if (board.PrintPriority != 12)
            //                        {
            //                            board.PrintPriority = 12;
            //                        }
            //                    }
            //                }
            //                if (photo.Status != (int)Status.PhotoStatus.Printed &&
            //                    photo.Status!= (int)Status.PhotoStatus.InGrid)
            //                {
            //                    photo.IsClearBox = true;
            //                }
                            
            //            }                       
            //        }
            //        db.SaveChanges();
            //        return 0;
            //    }
            //    catch (Exception ex)
            //    {
            //        return -1;
            //    }
            //}
        }

        
    }
}
